package cn.yudao.org.controller;

import cn.yudao.basic.annotation.PreAuthorize;
import cn.yudao.basic.exception.BusinessException;
import cn.yudao.basic.utils.ExcelUtil;
import cn.yudao.org.domain.ShopAuditLog;
import cn.yudao.org.service.IShopService;
import cn.yudao.org.domain.Shop;
import cn.yudao.org.query.ShopQuery;
import cn.yudao.basic.utils.AjaxResult;
import cn.yudao.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "新增/修改店铺",sn = "shop:addOrupt")
    public AjaxResult addOrupadte(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.upadte(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除店铺",sn = "shop:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get( @PathVariable("id")Long id)
    {
        return shopService.LoadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "查询店铺",sn = "shop:query")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        PageList query1 = shopService.query(query);
        return shopService.query(query);
    }

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            if (shop != null){
                shopService.settlement(shop);
            }
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
    * @Title: pass
    * @Description: 管理员审核通过
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/2 17:58
    * @Parameters:
    * @Return
    */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            if (shopAuditLog != null){
                shopService.pass(shopAuditLog);
            }
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
    * @Title:
    * @Description: 管理员驳回
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/2 17:57
    * @Parameters:
    * @Return
    */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            if (shopAuditLog != null){
                shopService.reject(shopAuditLog);
            }
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    // /shop/auditOut
    /**
    * @Title: auditOut
    * @Description: 管理员拒绝
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/3 12:59
    * @Parameters:
    * @Return
    */
    @PostMapping("/audit/out")
    public AjaxResult auditOut(@RequestBody ShopAuditLog shopAuditLog){
        try {
            if (shopAuditLog != null){
                shopService.auditOut(shopAuditLog);
            }
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
    * @Title: activation
    * @Description: 根据id激活当前店铺
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/2 19:51
    * @Parameters:
    * @Return
    */
    @PostMapping("/{id}")
    public AjaxResult activation(@PathVariable("id")Long id){
        try {
            if (id != null){
                shopService.activation(id);
            }
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    //List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,
    //                                   HttpServletResponse response

    /**
    * @Title: exportExcel
    * @Description: 导出
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/2 22:35
    * @Parameters:
    * @Return
    */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = shopService.loadAll();

        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xlsx",response);
    }

    /**
    * @Title: importExcel
    * @Description: 导入
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/2 22:35
    * @Parameters:
    * @Return
    */
    @PostMapping("/import/excel")
    public void importExcel(@RequestBody MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shopService.loadSave(shops);
    }
    
    /** 
    * @Title: drawColumnChart
    * @Description: echarts报表需要的数据（状态和数量）
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/3 8:54  
    * @Parameters: 
    * @Return 
    */
    @GetMapping("/drawColumnChart")
    public Map<String,List> drawColumnChart(HttpServletResponse response){
        Map<String, List> stringListMap = shopService.loadEcharts();
        return stringListMap;
    }

    @PostMapping("/update")
    public AjaxResult upadteChange(@RequestBody Shop shop){
        try {
            shopService.upadteChange(shop);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
    
}
